﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using Bussiness;

namespace GUI
{
    public partial class frmKhachHangMoi : Form
    {
        public frmKhachHangMoi()
        {
            InitializeComponent();
        }

        private void frmKhachHangMoi_Load(object sender, EventArgs e) {
            //load ma khach hang
            KhachHangService service = new KhachHangService();
            tbxMaKhachHang.Text = service.CreateMaKhachHang();
        }

        private void btnThoat_Click(object sender, EventArgs e) {
            Close();
        }

        private void btnRefresh_Click(object sender, EventArgs e) {
            tbxDiaChi.Text = "";
            tbxDienThoai.Text = "";
            tbxEmail.Text = "";
            tbxTenKhachHang.Text = "";
        }

        private void btnThemKhachHang_Click(object sender, EventArgs e) {
            //check info
            if (tbxTenKhachHang.Text.Trim() == "") {
                MessageBox.Show("Chưa nhập tên khách hàng");
                tbxTenKhachHang.Focus();
                return;
            }
            else {
                //create khach hang
                KhachHangDTO khachHang = new KhachHangDTO();
                khachHang.DiaChi = tbxDiaChi.Text;
                khachHang.DienThoai = tbxDienThoai.Text;
                khachHang.Email = tbxEmail.Text;
                khachHang.HoTen = tbxTenKhachHang.Text;
                khachHang.MaKhachHang = tbxMaKhachHang.Text;
                khachHang.TienNo = 0;
                //insert to database

                KhachHangService service = new KhachHangService();
                if (service.InsertNewKhachHang(khachHang)) {
                    MessageBox.Show("Thêm mới khách hàng thành công");
                    btnRefresh_Click(sender, e);                    
                }
                else
                    MessageBox.Show("Có lỗi, vui lòng thử lại");
            }
        }

        
    }
}
